using System;
using System.Collections.Generic;
using System.Linq;
using Tisbi.LawFirm.DAL.Models;

namespace Tisbi.LawFirm.BL.Services.Implementation
{
    public class LawsuitsServiceAgent : ServiceAgentBase<Lawsuits>, ILawsuitsServiceAgent
    {
        public void GetItemByCases(int caseId, Action<List<Lawsuits>, Exception> completed)
        {
            // Declare error and result
            Exception error = null;
            List<Lawsuits> items = null;

            // Set error or result
            try
            {
                if (caseId == 0)
                {
                    items = new List<Lawsuits>();
                    completed(items, null);
                    return;
                }
                List<Lawsuits> lawsuits = DataBaseContext.Cases.Where(x => x.Id == caseId).SelectMany(x => x.Lawsuitses).ToList();
                items = lawsuits.ToList();
            }
            catch (Exception ex)
            {
                error = ex;
            }

            // Invoke completion callback
            completed(items, error);
        }
    }
}